/*
 * loginAction.java
 *
 * Created on June 19, 2007, 5:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.twistinjava.action;

import com.twistinjava.logon.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.twistinjava.logonDAO.userManager;

/**
 * 
 * @author skumar
 */
public class loginAction extends DispatchAction
{
    private static Log log = LogFactory.getLog(loginAction.class);

	/** Creates a new instance of loginAction */
	public loginAction()
	{
	}

	public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
            log.debug("Inside login method of LoginAction ******");
		String loginID = request.getParameter("logonID");
		String password = request.getParameter("password");
		ActionMessages actionMessages = new ActionMessages();

		userManager usermManager = new userManager();

		usermManager.setLoginID(loginID);
		usermManager.setPassword(password);

		try
		{
			User user = usermManager.authenticateUser();

			if (user != null)
			{
				HttpSession session1 = request.getSession();
				session1.invalidate();
				HttpSession session = request.getSession(true);
				session.setAttribute("user", user);
				log.debug(loginID + " log in successfully.");
				return mapping.findForward("loginSuccess");
			} else
			{
				actionMessages.add("message", new ActionMessage("login.invalid"));
			}
		} catch (Exception e)
		{
			actionMessages.add("message", new ActionMessage("login.error.message", e.getMessage()));
		}

		saveMessages(request, actionMessages);

		return mapping.findForward("loginFailure");
	}

	public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
                log.debug("Inside logout method of loginAction class ********");
		ActionMessages actionMessages = new ActionMessages();
                HttpSession session = request.getSession(false);

		try
                {
                    if (session == null)
                    {
                        //if session is null then there can not be a logged in user.
                        log.debug("We have a null http session");


                        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("session.expired"));
                        saveMessages(request, actionMessages);


                    }
                    else
                    {
                        User user = (User) session.getAttribute("user");
			session.removeAttribute("user");
                        session.invalidate();
                        if(user != null)
                        {
                          String name =  user.getFirstName() + " " + user.getLastName();
			  actionMessages.add("message", new ActionMessage("logout.message", name));
                          saveMessages(request, actionMessages);
                        }
                        else
                        {
                        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("session.expired"));
                        saveMessages(request, actionMessages);

                        }
                       
                    }
		} catch (Exception e)
		{
			actionMessages.add("message", new ActionMessage("logout.error.message", e.getMessage()));
			saveMessages(request, actionMessages);
                        return mapping.findForward("showError");
		}

		
		return mapping.findForward("logout");
	}

}
